#ifndef FLOATING_MENU_WIDGET_H
#define FLOATING_MENU_WIDGET_H

#include <QWidget>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>
#include <QApplication>
#include <QTimer>
#include <QMouseEvent>
#include <QPropertyAnimation>
#include <QGraphicsDropShadowEffect>
#include <QShowEvent>
#include <QCloseEvent>
#include <QPaintEvent>
#include <QPainter>
#ifdef Q_OS_WIN
#include <windows.h>
#include <QWindow>
#endif
#include "roundmenuwidget.h"
#include "settings_dialog.h"

class FloatingMenuWidget : public QWidget
{
    Q_OBJECT

public:
    explicit FloatingMenuWidget(QWidget *parent = nullptr);
    ~FloatingMenuWidget();

protected:
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void paintEvent(QPaintEvent *event) override;  // 添加paintEvent处理透明背景
    void enterEvent(QEnterEvent *event) override;
    void leaveEvent(QEvent *event) override;
    void showEvent(QShowEvent *event) override;
    void closeEvent(QCloseEvent *event) override;
    
#ifdef Q_OS_WIN
    bool nativeEvent(const QByteArray &eventType, void *message, qintptr *result) override;
#endif

private slots:
    void onMenuItemClicked(int index);
    void onMenuToggled(bool expanded);
    void onTrayIconActivated(QSystemTrayIcon::ActivationReason reason);
    void onLongPressTimer();
    void onLayoutChanged(SettingsDialog::LayoutType layout);
    void showWindow();
    void hideWindow();
    void quitApplication();

private:
    void setupUI();
    void setupSystemTray();
    void setupMenuItems();
    void updateWindowMask();  // 新增：更新窗口遮罩
    void startLongPressTimer();
    void stopLongPressTimer();
    void enableDragMode();
    void disableDragMode();    void openSettingsDialog();  // 新增：打开设置对话框
    void applyLayoutSettings(SettingsDialog::LayoutType layout); // 新增：应用布局设置
    void loadSettings();         // 新增：加载设置
    void startNativeDrag();      // 新增：启动原生拖动
    void resetToCircularLayout();  // 重置为圆形布局

private:
    RoundMenuWidget *m_roundMenu;
    QSystemTrayIcon *m_trayIcon;
    QMenu *m_trayMenu;
      // 拖动相关
    bool m_isDragging;
    bool m_canDrag;
    QPoint m_dragStartPos;
    QTimer *m_longPressTimer;
    
    // Windows 原生拖动相关
#ifdef Q_OS_WIN
    bool m_draggingWithWinAPI;
    POINT m_winDragStartPos;
    HWND m_hwnd;
#endif
    
    // 动画效果
    QPropertyAnimation *m_opacityAnimation;
    QGraphicsDropShadowEffect *m_shadowEffect;
    
    // 配置
    static const int LONG_PRESS_DURATION = 500; // 长按时长（毫秒）
};

#endif // FLOATING_MENU_WIDGET_H
